﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Negocio;
using Business.Entities;

namespace UI.Web
{
    public partial class Usuarios : System.Web.UI.Page
    {
        UsuarioLogic _logic;
        

         public enum FormModes
        {
            Alta,
            Baja,
            Modificacion
        }

        public FormModes FormMode
        {
            get {return (FormModes)this.ViewState["FormMode"];}
            set { this.ViewState["FormMode"] = value; }
        }

        private Usuario Entity
        {
            get;
            set;
        }

        private int SelectedID
        {
        get
        {

        if (this.ViewState["SelectedID"]!=null)
        {
        return (int)this.ViewState["SelectedID"];
        }
        else
        {
        return 0;
        }

        }
        set
        {
        this.ViewState["SelectedID"] = value;
         }
         }

                private bool IsEntitySelected
                {
                    get
                    {
                        return (this.SelectedID != 0);
                    }
                }


        private UsuarioLogic Logic
        {
            get
            {
                if (_logic == null)
                {
                    _logic = new UsuarioLogic();
                }
                return _logic;

            }
        }

        private void LoadGrid()
        {
            this.gridView1.DataSource = this.Logic.getAll();
            this.gridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            btnAceptar.Text = "Aceptar";
           if (!Page.IsPostBack)
            {
                LoadGrid();
            }
            else
            {
               
            }
        }
        

        

        protected void gridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.formPanel1.Visible = false;
            this.SelectedID = (int)this.gridView1.SelectedValue;
           

        }

        private void LoadForm(int id)
        {
            this.Entity = this.Logic.getOne(id);
            this.nombreTextBox1.Text = this.Entity.Nombre;
            this.apellidoTextBox1.Text = this.Entity.Apellido;
            this.emailTextBox1.Text = this.Entity.Email;
            this.habilitadoCheckBox1.Checked = this.Entity.Habilitado;
            this.nombreUsuarioTextBox1.Text=this.Entity.Nombreusuario;
        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            if (this.FormMode == FormModes.Alta)
            {

                Entity = new Usuario();
                Entity.State = Usuario.States.New;
                _logic = new UsuarioLogic();
                this.Entity.Nombre = this.nombreTextBox1.Text;
                this.Entity.Apellido = this.apellidoTextBox1.Text;
                this.Entity.Email = this.emailTextBox1.Text;
                this.Entity.Habilitado = this.habilitadoCheckBox1.Checked;
                this.Entity.Nombreusuario = this.nombreUsuarioTextBox1.Text;
                this.Entity.Clave = this.claveTextBox1.Text; //falta validacion de clave.
                _logic.save(this.Entity);
                Page.Response.Redirect("Usuarios.aspx");
            }
            else if (this.FormMode == FormModes.Modificacion)
            {
                Entity = this.Logic.getOne(this.SelectedID);
                Entity.State = Usuario.States.Modified;
                _logic = new UsuarioLogic();
                this.Entity.Nombre = this.nombreTextBox1.Text;
                this.Entity.Apellido = this.apellidoTextBox1.Text;
                this.Entity.Email = this.emailTextBox1.Text;
                this.Entity.Habilitado = this.habilitadoCheckBox1.Checked;
                this.Entity.Nombreusuario = this.nombreUsuarioTextBox1.Text;
                this.Entity.Clave = this.claveTextBox1.Text; //falta validacion de clave.
                _logic.save(this.Entity);
                Page.Response.Redirect("Usuarios.aspx");
            }
            else
            {
                Entity = this.Logic.getOne(this.SelectedID);
                Entity.State = Usuario.States.Deleted;
                _logic = new UsuarioLogic();
                this.Entity.Nombre = this.nombreTextBox1.Text;
                this.Entity.Apellido = this.apellidoTextBox1.Text;
                this.Entity.Email = this.emailTextBox1.Text;
                this.Entity.Habilitado = this.habilitadoCheckBox1.Checked;
                this.Entity.Nombreusuario = this.nombreUsuarioTextBox1.Text;
                this.Entity.Clave = this.claveTextBox1.Text; //falta validacion de clave.
                _logic.save(this.Entity);
                Page.Response.Redirect("Usuarios.aspx");
            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("Usuarios.aspx");
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            this.formPanel1.Visible = true;
            this.FormMode = FormModes.Alta;
            this.gridActionsProfesores.Visible = false;
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {
            if (this.IsEntitySelected)
            {
                this.formPanel1.Visible = true;
                this.FormMode = FormModes.Modificacion;
                this.LoadForm(this.SelectedID);
                this.gridActionsProfesores.Visible = false;
            }
        }

        protected void btnEliminar_Click(object sender, EventArgs e)
        {
            if (this.IsEntitySelected)
            {
                this.formPanel1.Visible = true;
                this.FormMode = FormModes.Baja;
                this.LoadForm(this.SelectedID);
                btnAceptar.Text = "Eliminar";
                this.gridActionsProfesores.Visible = false;
            }
        }
    }
}
